package se.dat255.grupp12;
import android.app.Activity;
import android.content.Context;
import android.util.JsonReader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* Responsible class for saving lists locally when the application is being shut down and
* opening them when the it's reopened
* Created by David on 10/7/13.
*/
public class FileHandler {
private final GsonBuilder gsonBuilder = new GsonBuilder();
private final Gson gson = gsonBuilder.create();
private final String LISTFILE = "list_file";
private final String LOGFILE = "log_file";
private final String IPFILE = "ip_file";
public FileHandler(){}
/**
* Converts a list into a json formatted string ie. writes to the file
* @param list the list that is to be converted
* @param context the context from where the file should be written
* @throws IOException
*/
public void writeListToJson(List list, Context context, String fileName) throws IOException{
FileOutputStream fos;
String json = "";
if(fileName.equals(LISTFILE)){
fos = context.openFileOutput(LISTFILE, Context.MODE_PRIVATE);
json = gson.toJson(list, new TypeToken<ArrayList<TodoList>>(){}.getType());
}else{
fos = context.openFileOutput(LOGFILE, Context.MODE_PRIVATE);
json = gson.toJson(list, new TypeToken<LinkedList<Modification>>(){}.getType());
}
System.out.println(json+ "json");
fos.write(json.getBytes());
fos.close();
}
/**
* Saves the currently used IP address.
* @param context
* @throws IOException
*/
public void writeAddress(Context context) throws IOException {
FileOutputStream fos;
String json = "";
fos = context.openFileOutput(IPFILE, Context.MODE_PRIVATE);
json = gson.toJson(ServerConnection.getAddress());
fos.write(json.getBytes());
fos.close();
}
/**
* Creates a list of todolists or log from a json object/string ie. reads the file
* @param activity the activity in which the filewriting takes place
* @return the list of todolists extracted from the file
* @throws IOException if reading somehow fails
*/
public List readListFromJson(Activity activity, String fileName) throws IOException{
ArrayList<TodoList> list = new ArrayList<TodoList>();
FileInputStream fis;
try{
if(fileName.equals(LISTFILE)){
fis = activity.openFileInput(LISTFILE);
}else{
fis = activity.openFileInput(LOGFILE);
}
}catch(FileNotFoundException e){
return list;
}
byte content[] = new byte[fis.available()];
fis.read(content);
String json = new String(content);
com.google.gson.stream.JsonReader reader = new com.google.gson.stream.JsonReader(
new StringReader(json)
);
reader.setLenient(true);
if(fileName.equals(LISTFILE)){
list = gson.fromJson(reader, new TypeToken<ArrayList<TodoList>>(){}.getType());
return list;
}else{
LinkedList log = gson.fromJson(reader,
new TypeToken<LinkedList<Modification>>(){}.getType());
return log;
}
}
public void readAddressFromFile(Context activity) throws IOException {
String address = "";
FileInputStream fis = null;
try{
fis = activity.openFileInput(IPFILE);
byte content[] = new byte[fis.available()];
fis.read(content);
String json = new String(content);
com.google.gson.stream.JsonReader reader = new com.google.gson.stream.JsonReader(
new StringReader(json)
);
reader.setLenient(true);
address = gson.fromJson(reader,String.class);
ServerConnection.setAddress(address);
}catch(FileNotFoundException e){
ServerConnection.setAddress(address);
}catch(NullPointerException e){
ServerConnection.setAddress(address);
}
}
}